package com.quanyue.mechanics.complex.service.impl;

import com.quanyue.auto.util.AssertUtils;
import com.quanyue.mechanics.complex.mapper.JournalismMapper;
import com.quanyue.mechanics.complex.service.JournalismService;
import com.quanyue.mechanics.domain.entity.Journalism;
import com.vhuomai.mybatis.mapper.page.Page;
import com.vhuomai.mybatis.mapper.page.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class JournalismServiceImpl implements JournalismService {
    @Autowired
    private JournalismMapper journalismMapper;

    @Override
    public Journalism addJournalism(Journalism journalism) {
        journalism.setCreateTime(LocalDateTime.now());
        journalism.setUpdateTime(LocalDateTime.now());
        journalism.setDelFlag(false);
        journalismMapper.insert(journalism);
        return journalism;
    }

    @Override
    public void updateJournalism(Journalism journalism) {
        Journalism check = journalismMapper.findOne(journalism.getId());
        if (check == null) throw new AssertUtils.ValidateException("error news check");
        journalism.setCreateTime(check.getCreateTime());
        journalism.setUpdateTime(LocalDateTime.now());
        journalism.setDelFlag(false);
        journalismMapper.updateSelective(journalism);
    }

    @Override
    public void deleteJournalism(Integer id) {
        Journalism check = journalismMapper.findOne(id);
        if (check == null) throw new AssertUtils.ValidateException("error news check");
        journalismMapper.deleteOne(id);
    }

    @Override
    public Page <Journalism> findJournalism(PageRequest request) {
        request.setOrderBy("create_time");
        request.setAsc(false);
        return journalismMapper.findLimit(request);
    }
}
